unit Unit19; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Buttons, Unit1, Unit20, sqldb, LR_Class, Unit16, db, DbCtrls;

type

  { TForm19 }

  TForm19 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    ComboBox1: TComboBox;
    Datasource1: TDatasource;
    DBComboBox1: TDBComboBox;
    Edit1: TEdit;
    Edit2: TEdit;
    frReport1: TfrReport;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Memo1: TMemo;
    Memo2: TMemo;
    SQLQuery1: TSQLQuery;
    SQLQuery2: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure frReport1GetValue(const ParName: String; var ParValue: Variant);
  private
    codigo_incidente : String;
  public
    { public declarations }
  end; 

var
  Form19: TForm19; 

implementation

{ TForm19 }

procedure TForm19.FormShow(Sender: TObject);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  if SQLQuery2.Active = true then
    SQLQuery2.Close;
    
  SQLQuery2.SQL.Text := 'select fnc_nombre from funcionario where fnc_descripcion = ''TECNICO'' '
    + 'or fnc_descripcion = ''DEFAULT'' order by fnc_nombre';
  
  SQLQuery2.Open;
  
  DBComboBox1.Clear;
  DBComboBox1.DataField := 'fnc_nombre';
  
  while not SQLQuery2.EOF do
  begin
    DBComboBox1.Items.Add(SQLQuery2.Fields[0].AsString);
    SQLQuery2.Next;
  end;
  
  Edit1.Text := '';
  Edit2.Text := '';
  DBComboBox1.Text := '';
  Memo1.Text := '';
  Memo2.Text := '';
  Label7.Caption := '';
  codigo_incidente := '';
end;

procedure TForm19.frReport1GetValue(const ParName: String; var ParValue: Variant
  );
begin
  if ParName = 'codigo' then
    ParValue := codigo_incidente;

  if ParName = 'fecha_inicio' then
    ParValue := Date;

  if ParName = 'cliente' then
    ParValue := Label7.Caption;

  if ParName = 'titulo' then
    ParValue := Edit2.Text;

  if ParName = 'tecnico' then
    ParValue := DBComboBox1.Text;

  if ParName = 'estado' then
    ParValue := ComboBox1.Text;

  if ParName = 'detalle' then
    ParValue := Memo1.Text;

  if ParName = 'desarrollo' then
    ParValue := Memo2.Text;
end;

procedure TForm19.Button3Click(Sender: TObject);
begin
  Close;
end;

procedure TForm19.Button4Click(Sender: TObject);
begin
  Form16.ShowModal;
  
  Label7.Caption := Form16.Edit1.Text;
  Edit1.Text := Form16.codigo;
  
  Edit2.SetFocus;
end;

procedure TForm19.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  if SQLQuery2.Active = True then
    SQLQuery2.Close;
end;

procedure TForm19.FormCreate(Sender: TObject);
begin
  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;

  SQLQuery2.DataBase := Form1.PQConnection1;
  SQLQuery2.Transaction := Form1.SQLTransaction1;
end;

procedure TForm19.Button2Click(Sender: TObject);
var
  QueryString : String;
  
begin
  if(Edit1.Text <> '') and (ComboBox1.Text <> '') and (Edit2.Text <> '') and (Memo1.Text <> '')
    and (DBComboBox1.Text <> '') then
  begin
    QueryString := 'insert into incidente(inc_cliente, inc_titulo, inc_descripcion, inc_desarrollo, '
      + 'inc_estado, inc_responsable) values(' + Edit1.Text + ', ''' + Edit2.Text + ''', '''
      + Memo1.Text + ''', ''' + Memo2.Text + ''', ''' + ComboBox1.Text
      + ''', (select fnc_cdg from funcionario where fnc_nombre =''' + DBComboBox1.Text + '''))';

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    if SQLQuery1.Active = True then
      SQLQuery1.Close;
      
    SQLQuery1.SQL.Text := 'select max(inc_cdg) from incidente where inc_cliente = ' + Edit1.Text;

    SQLQuery1.Open;

    while not SQLQuery1.EOF do
    begin
      codigo_incidente := SQLQuery1.Fields[0].AsString;
      SQLQuery1.Next;
    end;

    SQLQuery1.Close;

    frReport1.LoadFromFile('reporte_incidencia.lrf');
    frReport1.ShowReport;

    Close;
  end
  else
    ShowMessage('ERROR EN EL INGRESO DE DATOS');
end;

procedure TForm19.Button1Click(Sender: TObject);
begin
  Form20.ShowModal;
  
  Edit1.Text := Form20.codigo_cliente;
  Label7.Caption := Form20.nombre_cliente;

  Edit2.SetFocus;
end;

initialization
  {$I unit19.lrs}

end.

